package com.sync.syncmasterdata.dingrobot.controller;

import com.sync.syncmasterdata.dingrobot.pojo.DingMsg;
import com.sync.syncmasterdata.dingrobot.service.DingAPIService;
import com.sync.syncmasterdata.dingrobot.service.DingRobotService;
import com.sync.syncmasterdata.customer.service.impl.UtilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 钉钉服务 controller层
 * 作废 统一使用工具类
 */
@Controller
@Deprecated
@RequestMapping("/dingapi")
public class DingController {

    @Autowired
    private DingAPIService dingAPIService;
    @Autowired
    private DingRobotService dingRobotService;

    @Autowired
    private UtilService utilService;

    /**
     * 手机号 + 信息 通过钉钉机器人传送
     * phones:[]
     * msg:[]
     */
    @PostMapping("/pushMsgByDing")
    @ResponseBody
    public void pushSimpleMsgByDing(@Validated @RequestBody DingMsg dingMsg) {
        List<String> users = this.dingAPIService.convertPhoneToUseridList(dingMsg.getPhones());
        Map<String, String> msgMap = new HashMap<>();
        msgMap.put("content", dingMsg.getMsg());
        //小哲推送消息
        this.dingRobotService.robotSendSampleMsg(msgMap, users);

    }

    @PostMapping("/pushImageByDing")
    @ResponseBody
    public void pushSimpleImageByDing(@Validated @RequestBody DingMsg dingMsg) {
        List<String> users = this.dingAPIService.convertPhoneToUseridList(dingMsg.getPhones());
        Map<String, String> msgMap = new HashMap<>();
        msgMap.put("photoURL", dingMsg.getMsg());
        //小哲推送消息
        this.dingRobotService.robotSendSampleImageMsg(msgMap, users);
    }

    @PostMapping("/pushLinkByDing")
    @ResponseBody
    public void pushSimpleLinkByDing(@Validated @RequestBody Map<String,Object> map) {
        List<String> users = this.dingAPIService.convertPhoneToUseridList((List) map.get("phones"));
        Map<String, String> msgMap = new HashMap<>();
        msgMap.put("title", map.get("title").toString());
        msgMap.put("text", map.get("text").toString());
        msgMap.put("picUrl",map.get("picUrl").toString());
        msgMap.put("messageUrl", map.get("messageUrl").toString());
        //小哲推送消息
        this.dingRobotService.robotSendSampleLinkMsg(msgMap, users);
    }

    /**
     * 通过phone 查找钉钉userid 接口
     * @param phone
     * @return
     */
    @PostMapping("/getuserid")
    @ResponseBody
    public String getDingUserid(@RequestParam(value = "phone") String phone) {
        return this.dingAPIService.getDingUseridByPhone(phone);
    }

    @PostMapping("/getToken")
    @ResponseBody
    public String getToken() {

        try {
            return this.utilService.getToken();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return null;
    }

    /**
     * 统一 异常返回，使用自定义httpcode 进行返回
     * @param e
     * @return
     * @throws Exception
     */
    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public String exceptionHandler(MethodArgumentNotValidException e) throws Exception {
        return e.getBindingResult().getFieldError().getDefaultMessage();
    }
}
